package com.shaoshaohuo.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.view.TopbarView;

public class EvaluationActivity extends BaseActivity implements OnClickListener {

	public static final int EVALUATION_TYPE_SUPPLY = 1;
	public static final int EVALUATION_TYPE_PURCHASE = 2;
	public static final int EVALUATION_TYPE_WULIE = 3;
	
	private TopbarView mTopbarView;

	private RatingBar ratingBar;
	private EditText mContentEdit;
	private TextView mTextView1;
	private TextView mTextView2;
	private TextView mTextView3;
	private TextView mTextView4;
	private Button mButton;

	private String orderid;
	private int evaluationType = EVALUATION_TYPE_WULIE; 

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_evaluation);
		initData();
		initView();
		setUpView();
	}

	private void initData() {
		orderid = getIntent().getStringExtra(ExtraName.orderid);
		evaluationType = getIntent().getIntExtra(ExtraName.type, EVALUATION_TYPE_WULIE);
	}

	private void initView() {
		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);

		ratingBar = (RatingBar) this.findViewById(R.id.ratingbar);
		mContentEdit = (EditText) this.findViewById(R.id.edittext_content);

		mTextView1 = (TextView) this.findViewById(R.id.textview_henzhunshi);
		mTextView2 = (TextView) this.findViewById(R.id.textview_henfuze);
		mTextView3 = (TextView) this.findViewById(R.id.textview_renpinhenhao);
		mTextView4 = (TextView) this.findViewById(R.id.textview_jiagegongdao);
		mButton = (Button) this.findViewById(R.id.button_ok);
	}

	private void setUpView() {
		mTopbarView.setCenterText("评价");
		mTopbarView.setLeftView(true, true);

		mTextView1.setOnClickListener(this);
		mTextView2.setOnClickListener(this);
		mTextView3.setOnClickListener(this);
		mTextView4.setOnClickListener(this);
		mButton.setOnClickListener(this);

		switch (evaluationType) {
		case EVALUATION_TYPE_WULIE:
			
			break;
		case EVALUATION_TYPE_SUPPLY:
			mTextView1.setText("付款及时");
			mTextView2.setText("有信用");
			mTextView3.setVisibility(View.GONE);
			mTextView4.setVisibility(View.GONE);
			break;
		case EVALUATION_TYPE_PURCHASE:
			mTextView1.setText("货很新鲜");
			mTextView2.setText("态度好");
			mTextView2.setText("价格公道");
			mTextView4.setVisibility(View.GONE);
			break;
		}
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button_ok:
			evaluation();
			break;
		case R.id.textview_henzhunshi:
			appendText(" "+mTextView1.getText().toString()+" ");
			break;
		case R.id.textview_henfuze:
			appendText(" "+mTextView2.getText().toString()+" ");
			break;
		case R.id.textview_renpinhenhao:
			appendText(" "+mTextView3.getText().toString()+" ");
			break;
		case R.id.textview_jiagegongdao:
			appendText(" "+mTextView4.getText().toString()+" ");
			break;
		}
	}

	private void appendText(String text) {
		Editable editableText = mContentEdit.getEditableText();
		editableText.append(text);
		mContentEdit.setText(editableText);
		mContentEdit.setSelection(editableText.length());
	}

	private void evaluation() {
		
		int rating = (int) ratingBar.getRating();
		if(rating <= 0){
			showToast("请打分");
			return;
		}
		
		String content = mContentEdit.getText().toString().trim();
		if(TextUtils.isEmpty(content)){
			showToast("请输入评价内容");
			return;
		}
		
		if(evaluationType == EVALUATION_TYPE_PURCHASE || evaluationType == EVALUATION_TYPE_SUPPLY ){
			startLoadingDialog();
			RequestService.getInstance().evaluationEc(this, orderid, content, evaluationType+"",rating+"", BaseEntity.class, new RequestListener() {
				
				@Override
				public void onSuccess(int requestCode, BaseEntity resultData) {
					dismissLoadingDialog();
					if(resultData.isOk()){
						showToast("评价成功");
						Intent intent = new Intent(EvaluationActivity.this,MainTabActivity.class);
						startActivity(intent);
					}else{
						showToast(resultData.getMsg());
					}
				}
				
				@Override
				public void onFailed(int requestCode, Exception error, String msg) {
					dismissLoadingDialog();
					showToast(R.string.please_check_network);
				}
			});
		}else{
			startLoadingDialog();
			RequestService.getInstance().evaluation(this, orderid, content, rating+"", BaseEntity.class, new RequestListener() {
				
				@Override
				public void onSuccess(int requestCode, BaseEntity resultData) {
					dismissLoadingDialog();
					if(resultData.isOk()){
						showToast("评价成功");
						Intent intent = new Intent(EvaluationActivity.this,MainTabActivity.class);
						startActivity(intent);
					}else{
						showToast(resultData.getMsg());
					}
				}
				
				@Override
				public void onFailed(int requestCode, Exception error, String msg) {
					dismissLoadingDialog();
					showToast(R.string.please_check_network);
				}
			});
		}
		
	}
	
	private void evaluationEc() {
		
		int rating = (int) ratingBar.getRating();
		if(rating <= 0){
			showToast("请打分");
			return;
		}
		
		String content = mContentEdit.getText().toString().trim();
		if(TextUtils.isEmpty(content)){
			showToast("请输入评价内容");
			return;
		}
		
		startLoadingDialog();
		RequestService.getInstance().evaluationEc(this, orderid, content, rating+"",rating+"", BaseEntity.class, new RequestListener() {
			
			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				dismissLoadingDialog();
				if(resultData.isOk()){
					showToast("评价成功");
					Intent intent = new Intent(EvaluationActivity.this,MainTabActivity.class);
					startActivity(intent);
				}else{
					showToast(resultData.getMsg());
				}
			}
			
			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				dismissLoadingDialog();
				showToast(R.string.please_check_network);
			}
		});
	}
}
